package com.ruoyi.charge.model;

import com.ruoyi.common.enums.GunStatusEnum;
import lombok.Data;

/**
 * 充电枪实时数据 DTO
 */
@Data
public class ChargerGunData {
    private int number; // 枪号
    private GunStatusEnum status = GunStatusEnum.IDLE; // 默认为空闲
    private boolean isHolstered = false; // 是否归位
    private boolean isConnected = true; // 是否插枪
    private double outputVoltage = 0.0; // 输出电压 (V)
    private double outputCurrent = 0.0; // 输出电流 (A)
    private int cableTemperature = 10; // 枪线温度 (摄氏度)
    private int soc = 0; // SOC (%)
    private int batteryMaxTemperature = 25; // 电池组最高温度 (摄氏度)
    private int remainingTime = 0; // 剩余时间 (分钟)
    private short hardwareFault = 0; // 硬件故障

    // 模拟参数：目标SOC（通常为100）
    private final int TARGET_SOC = 100;

    // 模拟参数：初始剩余时间（单位：分钟），可根据功率估算
    private int initialRemainingTime = 60; // 假设充满需60分钟

    // 充电开始时间（用于计算已充电量）
    private long chargingStartTime = -1;

    // 是否正在充电（用于控制更新）
    private boolean isCharging = false;
    // 在 ChargerGunData 中添加
    private long lastUpdateTime = System.currentTimeMillis();

    public void startCharging() {
        this.status = GunStatusEnum.CHARGING;
        this.isConnected = true;
        this.isHolstered = false;
        this.outputVoltage = 225.5;
        this.outputCurrent = 15.2;
        this.soc = Math.max(10, this.soc); // 初始SOC不低于10%
        this.remainingTime = initialRemainingTime;
        this.chargingStartTime = System.currentTimeMillis();
        this.isCharging = true;
    }

    public void stopCharging() {
        this.status = GunStatusEnum.IDLE;
        this.outputVoltage = 0.0;
        this.outputCurrent = 0.0;
        this.isConnected = false;
        this.isCharging = false;
        this.chargingStartTime = -1;

    }

    /**
     * 根据时间差（毫秒）更新充电状态：SOC、remainingTime、温度等
     */
    public void updateForCharging(long deltaTimeMs) {
        if (!isCharging || status != GunStatusEnum.CHARGING) return;

        double deltaSeconds = deltaTimeMs / 1000.0;
        double deltaMinutes = deltaTimeMs / 60000.0;

        // 1. 计算总充电时间（分钟）
        long elapsedMinutes = (System.currentTimeMillis() - chargingStartTime) / 60000;
        int chargedMinutes = (int) Math.min(elapsedMinutes, initialRemainingTime);

        // 2. 线性模拟 SOC 上升（可根据电池曲线优化）
        this.soc = Math.min(TARGET_SOC, (int) (100.0 * chargedMinutes / initialRemainingTime));
        this.soc = Math.max(this.soc, 10); // 防止异常

        // 3. 剩余时间 = 总时间 - 已充电时间
        this.remainingTime = Math.max(0, initialRemainingTime - chargedMinutes);

        // 4. 温度缓慢上升（模拟发热）
        if (deltaTimeMs > 5000) { // 每5秒上升1度
            this.cableTemperature = Math.min(85, this.cableTemperature + 1);
            this.batteryMaxTemperature = Math.min(60, this.batteryMaxTemperature + 1);
        }

        // 5. 如果 SOC 达到 100%，自动停止充电
        if (this.soc >= TARGET_SOC) {
            this.soc = TARGET_SOC;
            this.remainingTime = 0;
            stopCharging();
        }
    }

    // 可选：设置初始剩余时间（外部可配置）
    public void setInitialRemainingTime(int minutes) {
        this.initialRemainingTime = Math.max(1, minutes);
    }
}